package com.algorithm3;

import java.util.Arrays;

public class BubbleSort {

    public static void sort(int[] array) {
        for(int i=0;i<array.length-1;i++){
            boolean changed  = false;
            for(int j=0;j<array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    changed = true;
                    swap(array,j,j+1);
                }
            }
            if(!changed){
                return;
            }
            System.out.println("排序:"+i+"-->"+ Arrays.toString(array));
        }
    }

    public static void swap(int[] array, int i, int j) {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
    }

    public static void main(String[] args){
        int[] array = new int[]{5,7,6,8,9,3,2,4,1,10};
        System.out.println("排序前:"+ Arrays.toString(array));
        sort(array);
        System.out.println("排序后:"+ Arrays.toString(array));
    }
}
